<?php
	class Model extends database{	
		public function select($table="",$where="",$order="",$data="")
		{
                        $sql1 = array();
			if($data =="")
			{
				$data = "*";
			}
			if($where == "")
			{	
				$sql = "select ".$data." from ".$table;
			}
			else
			{
				if(is_array($where))
				{
					$sql = "select ".$data." from ".$table." where ";
					foreach($where as $lw=>$rw)
					{
						if(is_string($rw))
						{
							$sql1[] .= $lw." = '".$rw."'";
						}
						else
						{
							$sql1[] .= $lw." = ".$rw;
						}
					}
					$sql .= implode(" and ",$sql1);
				}
				else
				{
					$sql = "select ".$data." from ".$table." where ".$where;
				}
			}
			if($order != "")
				$sql .= " ".$order;
			$this->setSql($sql);			
			$this->query(); //echo $sql."<br>";
			return $this->fetchArray();
		}		
		public function insert($table,$value)
		{
			if(is_array($value))
			{
				foreach($value as $lv=>$rv)
				{
					$sql1[] = $lv;
					if(is_string($rv))
					{
						$sql2[] = "'".$rv."'";
					}
					else
					{
						$sql2[] = $rv;
					}
				}
				$sql1 = implode(",",$sql1);
				$sql2 = implode(",",$sql2);
				$sql = "insert into ".$table."(".$sql1.") values(".$sql2.")";
			}
			else
			{
				$sql = "insert into ".$table." values(".$value.")";
			}
			$this->setSql($sql); //dumpvar($sql);                        
			$this->query();
		}
		public function delete($table,$where="")
		{
			if($where == "")
			{
				$sql = "delete from ".$table;
			}
			else
			{
				if(is_array($where))
				{
					$sql = "delete from ".$table." where ";
					foreach($where as $lw=>$rw)
					{
						if(is_string($rw))
						{
							$sql1[] = $lw." = '".$rw."'";
						}
						else
						{
							$sql1[] = $lw." = ".$rw;
						}
					}
					$sql .= implode(" and ",$sql1);
				}
				else
				{
					$sql = "delete from ".$table." where ".$where;
				}
			}
			$this->setSql($sql); //dumpvar($sql);		
			$this->query();
		}
		public function update($table,$value="",$key="",$string="")
		{
			if($string != "")
			{
				$sql = "update ".$table." set ".$string;
			}
			else
			{
				if(is_array($value))
				{
					$sql = "update ".$table." set ";
					foreach($value as $lv=>$rv)
					{
						if(is_string($rv))
						{
							$sql1[] = $lv." = '".$rv."'";
						}
						else
						{
							$sql1[] = $lv." = ".$rv;
						}
					}
					$sql .= implode(", ",$sql1);					
				}
				else
				{
					$sql = "update ".$table." set ".$value;
				}
				if(is_array($key))
				{
					$sql .= " where ";
					foreach($key as $lv=>$lr)
					{
						if(is_string($lr))
						{
							$sql2[] = $lv." = '".$lr."'";
						}
						else
						{
							$sql2[] = $lv." = ".$lr;
						}
					}
					$sql .= implode(" and ",$sql2);
				}
				else
				{
					$sql .= " where ".$key;
				}
			}			
			$this->setSql($sql); //dumpvar($sql);
                        
			$this->query();
		}
	}
?>